package com.rain.friend.controller;

import com.rain.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/friend")
public class FrinedController {

    @Autowired
    private FriendService friendService;

    /**
     * 添加好友
     * @param friendid
     * @param  type  当是1的时候是粉  当是0的时候是黑
     * @return
     */
    @RequestMapping(value="/like/{friendid}/{type}",method = RequestMethod.PUT )
    public Result addFriend(@PathVariable("friendid") String friendid, @PathVariable("type") String type, HttpServletRequest request){
        //1.获取请求域中的登录信息
        Claims claims = (Claims) request.getAttribute("user_claims");
        if(claims == null){
            return new Result(false, StatusCode.ACCESS_ERROR,"没有权限");
        }
        //2.判断是加粉还是拉黑
        if("1".equals(type)){
            //加粉
            int res = friendService.addFriend(claims.getId(),friendid);
            if(res == 0){
                return new Result(false,StatusCode.REP_ERROR,"请不要重复操作");
            }
        }else{
            //拉黑
            int res = friendService.addNoFriend(claims.getId(),friendid);
            if(res == 0){
                return new Result(false,StatusCode.REP_ERROR,"请不要重复操作");
            }
        }

        return new Result(true,StatusCode.OK,"操作成功");
    }

    /**
     * 删除好友
     * @param friendid
     * @param request
     * @return
     */
    @RequestMapping(value="/{friendid}",method = RequestMethod.DELETE )
    public Result deleteFriend(@PathVariable("friendid") String friendid, HttpServletRequest request){
        //1.获取请求域中的登录信息
        Claims claims = (Claims) request.getAttribute("user_claims");
        if(claims == null){
            return new Result(false, StatusCode.ACCESS_ERROR,"没有权限");
        }
        //2.删除好友
        friendService.deleteFriend(claims.getId(),friendid);

        return new Result(true,StatusCode.OK,"操作成功");
    }
}
